package com.example.wheelview;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * 摘要		 轮		适配器 		提供		  常见		功能				 适配器
 */
public abstract class AbstractWheelTextAdapter extends AbstractWheelAdapter {
    
    /** 
     * TextView资源				(适配器默认视图)
     * 	Text view resource. Used as a default view for adapter. 
     */
    public static final int TEXT_VIEW_ITEM_RESOURCE = -1;
    
    /** No resource constant	无资源常量 */
    protected static final int NO_RESOURCE = 0;
    
    /** Default text color 默认文本颜色 */
    public static final int DEFAULT_TEXT_COLOR = 0xFF585858;
    /** Default label color 默认标签颜色 */
    public static final int LABEL_COLOR = 0xFF700070;
    /** Default text size 默认文本字体大小 */
    public static final int DEFAULT_TEXT_SIZE = 18;
    
    /** 文本颜色 */
    private int textColor = DEFAULT_TEXT_COLOR;
    /** 文本字体大小 */
    private int textSize = DEFAULT_TEXT_SIZE;
    
    /** Current context 当前上下文 */
    protected Context context;
    /** Layout inflater 反射器 */
    protected LayoutInflater inflater;
    
    /** itemResourceId 				item资源id */
    protected int itemResourceId;
    /** itemTextResourceId			 item文本资源id */
    protected int itemTextResourceId;
    /** emptyItemResourceId		 item空资源id */
    protected int emptyItemResourceId;
	
    /**
     * 构造方法1
     * @param context 上下文
     */
    protected AbstractWheelTextAdapter(Context context) {
        this(context, TEXT_VIEW_ITEM_RESOURCE);
    }

    /**
     * 构造方法2
     * @param context 				上下文
     * @param itemResource 	item资源(id)
     */
    protected AbstractWheelTextAdapter(Context context, int itemResource) {
        this(context, itemResource, NO_RESOURCE);
    }
    
    /**
     * 构造方法3
     * @param context 					上下文
     * @param itemResource			item资源(id)
     * @param itemTextResource 	item文本资源(id)
     */
    protected AbstractWheelTextAdapter(Context context, int itemResource, int itemTextResource) {
        this.context = context;
        itemResourceId = itemResource;
        itemTextResourceId = itemTextResource;
        
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    
    /**  get文本颜色 */
    public int getTextColor() {
        return textColor;
    }
    /** set文本颜色 */
    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }
    
    
    /** get字体大小 */
    public int getTextSize() {
        return textSize;
    }
    /** set字体大小 */
    public void setTextSize(int textSize) {
        this.textSize = textSize;
    }
    
    
    /** get item资源id */
    public int getItemResource() {
        return itemResourceId;
    }
    /** set item资源id */
    public void setItemResource(int itemResourceId) {
        this.itemResourceId = itemResourceId;
    }
    
    
    /** get item 文本资源id */
    public int getItemTextResource() {
        return itemTextResourceId;
    }
    /** set item 文本资源id */
    public void setItemTextResource(int itemTextResourceId) {
        this.itemTextResourceId = itemTextResourceId;
    }

    
    /** get item空资源id */
    public int getEmptyItemResource() {
        return emptyItemResourceId;
    }
    /** set item空资源id */
    public void setEmptyItemResource(int emptyItemResourceId) {
        this.emptyItemResourceId = emptyItemResourceId;
    }
    
    
    /**
     * @param index 指数
     * @return the text of specified items	返回指定文本
     */
    protected abstract CharSequence getItemText(int index);

    public View getItem(int index, View convertView, ViewGroup parent) {
    	
        if (index >= 0 && index < getItemsCount()) {
        	
            if (convertView == null) {
                convertView = getView(itemResourceId, parent);
            }
            TextView textView = getTextView(convertView, itemTextResourceId);
            
            if (textView != null) {
                CharSequence text = getItemText(index);
                if (text == null) {
                    text = "";
                }
                textView.setText(text);
    
                if (itemResourceId == TEXT_VIEW_ITEM_RESOURCE) {
                    configureTextView(textView);
                }
            }
            return convertView;
        }
    	return null;
    }

    public View getEmptyItem(View convertView, ViewGroup parent) {
    	
        if (convertView == null) {
            convertView = getView(emptyItemResourceId, parent);
        }
        
        if (emptyItemResourceId == TEXT_VIEW_ITEM_RESOURCE && convertView instanceof TextView) {
            configureTextView((TextView)convertView);
        }
            
        return convertView;
	}

    /** set 文本视图 */
    protected void configureTextView(TextView view) {
        view.setTextColor(textColor);
        view.setGravity(Gravity.CENTER);
        view.setTextSize(textSize);
        view.setEllipsize(TextUtils.TruncateAt.END);
        view.setLines(1);
//        view.setCompoundDrawablePadding(20);
//        view.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
    }
    
    /** get 文本视图 */
    public TextView getTextView(View view, int textResource) {
    	TextView text = null;
    	try {
            if (textResource == NO_RESOURCE && view instanceof TextView) {
                text = (TextView) view;
            } else if (textResource != NO_RESOURCE) {
                text = (TextView) view.findViewById(textResource);
            }
        } catch (ClassCastException e) {
            Log.e("AbstractWheelAdapter", "You must supply a resource ID for a TextView");
            throw new IllegalStateException(
                    "AbstractWheelAdapter requires the resource ID to be a TextView", e);
        }
        return text;
    }
    
    /** getView */
    public View getView(int resource, ViewGroup parent) {
        switch (resource) {
        case NO_RESOURCE:
            return null;
        case TEXT_VIEW_ITEM_RESOURCE:
            return new TextView(context);
        default:
            return inflater.inflate(resource, parent, false);    
        }
    }
    
}